﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Auditing;
using Abp.Web.Models;
using FwProject.Controllers;
using FwProject.Shop;
using FwProject.Shop.Dto;
using FwProject.ShoppingMall.Shop;
using Microsoft.AspNetCore.Mvc;

namespace FwProject.Web.Shop.Controllers
{
    /// <summary>
    /// 标签控制器
    /// </summary>
    public class TagController : FwProjectControllerBase
    {
        private readonly IShopTagAppService _shopTagAppService;

        private readonly IShopProductTagAppService _shopProductTagAppService;
        public TagController(IShopTagAppService shopTagAppService, IShopProductTagAppService shopProductTagAppService)
        {
            _shopTagAppService = shopTagAppService;
            _shopProductTagAppService = shopProductTagAppService;
        }


        /// <summary>
        /// 获取标签列表数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetTagData(int page, int limit, TagTypeEnum? tagType)
        {
            var data = await _shopTagAppService.GetLayerDataAsync(page, limit, tagType);

            return Json(data);
        }


        #region 标签列表
        /// <summary>
        /// 标签列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        public async Task<IActionResult> SwitchActive(Guid id, bool active)
        {
            await _shopTagAppService.SwitchActive(id, active);
            return Json(id);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Create(ShopTagInput input)
        {
            var id = await _shopTagAppService.CreateAsync(input);
            return Json(id);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> Edit(Guid id)
        {
            ViewBag.Id = id;
            var dto = await _shopTagAppService.GetEnterDtoAsync(id);
            return View(dto);
        }
        [HttpPost]
        public async Task<IActionResult> Edit(Guid id, ShopTagInput input)
        {
            await _shopTagAppService.ModifyAsync(id, input);
            return Json(id);
        }


        public async Task<IActionResult> Delete(Guid id)
        {
            await _shopTagAppService.DeleteAsync(id);
            return Json(id);
        }
        #endregion

        #region 标签商品设置
        /// <summary>
        /// 标签商品设置
        /// </summary>
        /// <returns></returns>
        public IActionResult TagShopSet()
        {
            return View();
        }

        /// <summary>
        /// 设置商品标签
        /// </summary>
        /// <returns></returns>
        public IActionResult SetShop(Guid tagid)
        {
            ViewBag.TagId = tagid;
            return View();
        }

        public IActionResult AddShopTag(Guid tagId)
        {
            ViewBag.TagId = tagId;
            return View();
        }

        /// <summary>
        /// 删除商品绑定标签
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> DeleteShopProductTag(Guid id)
        {
            await _shopProductTagAppService.DeleteAsync(id);
            return Json(id);
        }

        /// <summary>
        /// 添加商品绑定标签（批量）
        /// </summary>
        /// <param name="tagid"></param>
        /// <param name="shopids"></param>
        /// <returns></returns>
        public async Task<IActionResult> CreateShopProductTag(Guid tagid, string shopids)
        {
            await _shopProductTagAppService.CreateAsync(tagid, shopids);
            return Json("添加成功");
        }

        /// <summary>
        /// 获取已添加该标签的商品信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="tagid"></param>
        /// <returns></returns>
        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetSetShopData(int page, int limit, Guid tagid)
        {
            var data = await _shopProductTagAppService.GetShopProductTagByTagid(page, limit, tagid);
            return Json(data);
        }

        /// <summary>
        /// 获取未添加该标签的商品
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="tagid"></param>
        /// <returns></returns>
        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetNotTagShopData(int page, int limit, Guid tagid)
        {
            var data = await _shopProductTagAppService.GetNotTagShopDataByTagid(page, limit, tagid);
            return Json(data);
        }


        #endregion

        #region 标签品牌设置
        /// <summary>
        /// 标签品牌设置
        /// </summary>
        /// <returns></returns>
        public IActionResult TagBrandsSet()
        {
            return View();
        }

        /// <summary>
        /// 设置品牌标签
        /// </summary>
        /// <returns></returns>
        public IActionResult SetBrands(Guid tagid)
        {
            ViewBag.TagId = tagid;
            return View();
        }

        /// <summary>
        /// 获取已添加该标签的商品信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="tagid"></param>
        /// <returns></returns>
        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetAlreadyBrandsData(int page, int limit, Guid tagid)
        {
            var data = await _shopProductTagAppService.GetShopProductTagByTagid(page, limit, tagid);
            return Json(data);
        }
        #endregion
    }
}